﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BLL;

namespace NCRE学生考试端V1._0.教师端操作类
{
    public partial class UCTeacherSelect : UserControl
    {
        SelectQuestionBll sqBLL = new SelectQuestionBll();
        string rightAnswer = string.Empty;

        public UCTeacherSelect()
        {
            InitializeComponent();
        }

        private string value = string.Empty;
        public string Value
        {
            get { return this.value; }
            set
            {
                this.value = value;

            }
        }

        /// <summary>
        /// 将信息加载到控件上
        /// </summary>
        /// <param name="qe"></param>
        /// <param name="count"></param>
        public void BindDataToSelf(SelectQuestionRecordEntity qe, int count)
        {

            try
            {
                int num = 0;
                Choice2.Name = qe.QuestionID;

                //将已有的答题记录 显示到用户控件的RadioButton上，用户数据会先
                if (qe.ExamAnswer != string.Empty)
                {

                    //循环GroupBox上的控件
                    foreach (Control item in Choice2.Controls)
                    {
                        //如果、RadionButton，那么将正确答案信息显示到相应的RadionButton上
                        if (item is RadioButton && qe.ExamAnswer == item.Text)
                        {
                            RadioButton rb = (RadioButton)item;
                            rb.Checked = true;
                        }
                    }

                }
             
            }
            catch (Exception)
            {
                System.Environment.Exit(0);

            };
        }

        //选择题作答过程
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                this.value = radioButton1.Text;
                int numFlag = UpdateSelectQuestionRecord(value);
                if (numFlag == 0)
                {
                    MessageBox.Show(this, "信息保存失败，请联系管理员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    return;
                }

                UCTeacherSelect us = this;

                foreach (Label key in FrmMain.dict.Keys)
                {
                    if (FrmMain.dict[key].Name.ToString().Equals(this.Name))
                    {
                        key.BackColor = Color.LightGray;
                    }
                }

            }
            catch (Exception)
            {
                System.Environment.Exit(0);

            }

        }

        //选择题作答过程
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                this.value = radioButton2.Text;
                int numFlag = UpdateSelectQuestionRecord(value);
                if (numFlag == 0)
                {
                    MessageBox.Show(this, "信息保存失败，请联系管理员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    return;
                }

                UCTeacherSelect us = this;
                foreach (Label key in FrmMain.dict.Keys)
                {
                    if (FrmMain.dict[key].Name.ToString().Equals(this.Name))
                    {
                        key.BackColor = Color.LightGray;
                    }
                }
            }
            catch (Exception)
            {

                System.Environment.Exit(0);
            }

        }

        //选择题作答过程
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                this.value = radioButton3.Text;
                int numFlag = UpdateSelectQuestionRecord(value);
                if (numFlag == 0)
                {
                    MessageBox.Show(this, "信息保存失败，请联系管理员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    return;
                }

                UCTeacherSelect us = this;
                foreach (Label key in FrmMain.dict.Keys)
                {
                    if (FrmMain.dict[key].Name.ToString().Equals(this.Name))
                    {
                        key.BackColor = Color.LightGray;
                    }
                }
            }
            catch (Exception)
            {
                System.Environment.Exit(0);

            }


        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                this.value = radioButton4.Text;
                int numFlag = UpdateSelectQuestionRecord(value);
                if (numFlag == 0)
                {
                    MessageBox.Show(this, "信息保存失败，请联系管理员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    return;
                }

                UCTeacherSelect us = this;
                foreach (Label key in FrmMain.dict.Keys)
                {
                    if (FrmMain.dict[key].Name.ToString().Equals(this.Name))
                    {
                        key.BackColor = Color.LightGray;
                    }
                }
            }
            catch (Exception)
            {
                System.Environment.Exit(0);

            }

        }

        #region 保存答题记录
        /// <summary>
        /// 保存答题记录
        /// </summary>
        /// <param name="strAnswer"></param>
        /// <returns></returns>
        public int UpdateSelectQuestionRecord(string strAnswer)
        {
            try
            {
                //学生答题信息保存
                StudentInfoEntity st = FrmMain.st;

                SelectQuestionRecordEntity sqRecord = new SelectQuestionRecordEntity();

                sqRecord.QuestionID = Choice2.Name;
                sqRecord.ExamAnswer = strAnswer;
                int numFlag = sqBLL.UpdateSelectQuestionRecordByStudentInfo(st, sqRecord, rightAnswer);
                if (numFlag == 0)
                {
                    MessageBox.Show(this, "111系统错误，请联系管理员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            catch (Exception)
            {
                return 0;

            }

        }

        #endregion


    }
}
